<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //核心算法：利用charAt() 遍历这个字符串
        //把每个字符串都储存给对象，如果对象没有该属性：就为1，如果存在了就+1
        //遍历对，得到最大值和该字符
        var str = 'dwqfewcwdsadwfqfsfwgtg';
        var o = {};
        /*  for (var i = 0; i < str.length; i++) {
          var chars = str.charAt(i);
          if(o[chars]) {
              o[chars]++;
          }else{
              o[chars] = 1;
          }
         }
         console.log(o);
         //2，遍历对象
         var max = 0;
         var ch = '';
         for(var k in o) {
             if(o[k] > max) {
                max = o[k];
                ch = k;
             }
         }
         console.log(max);
         console.log(ch); */




        for (var i = 0; i < str.length; i++) {
            var str1 = str.charAt(i)
            //console.log(str1)
            if (o[str1]) {
                o[str1]++
            } else {
                o[str1] = 1;
            }
        }
        console.log(o)

        var max = 0;
        var cha = '';
        for (var k in o) {
            if (o[k] > max) {
                max = o[k]; // o[k] 得到的是属性值
                cha = k;   // k 得到是 属性名
            }
        }
        console.log(max);
        console.log(cha);
    </script>
</body>

</html>